﻿using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using BASIC_CORE.Domain.Caching.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.ServiceStackRedis;

namespace BASIC_REDIS_API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RedisController : ControllerBase
    {
        private readonly IServiceStackRedisCache _serviceStackRedisCache;
        private readonly IServiceStackRedisCacheRepository _serviceStackRedisCacheRepository;

        public RedisController(IServiceStackRedisCache serviceStackRedisCache, IServiceStackRedisCacheRepository serviceStackRedisCacheRepository)
        {
            _serviceStackRedisCache = serviceStackRedisCache;
            _serviceStackRedisCacheRepository = serviceStackRedisCacheRepository;
        }

        [HttpGet("Get")]
        public async Task<IActionResult> Get()
        {
            return Ok("");
        }

        [HttpPost("Set")]
        public async Task<IActionResult> Set([Required] string key, [Required] string value)
        {
            await _serviceStackRedisCacheRepository.Set(key, value, 3600);
            return Ok("ok");
        }

        [HttpPost("HashSet")]
        public async Task<IActionResult> HashSet([Required] string hashId, [Required] string key, [Required] string value)
        {
            await _serviceStackRedisCacheRepository.SetEntryInHashIfNotExists(hashId, key, value);
            return Ok("ok");
        }

        [HttpDelete("Delete")]
        public async Task<IActionResult> Delete([Required] string key)
        {
            _serviceStackRedisCache.Remove(key);
            return Ok("ok");
        }

    }
}
